#include "scaler.h"

#include <malloc.h>

Scaler::~Scaler()
{
    this->clean();
}

void Scaler::setIndex(std::string index_value)
{
    this->index = index_value;
}

void Scaler::setItems(std::map<std::string, std::string> items_value)
{
    this->items = items_value;
    // std::map<std::string, std::string>().swap(items_value);
    // malloc_trim(0);
}

void Scaler::addItem(std::string key, std::string value)
{
    this->items[key] = value;
}

std::string Scaler::getIndex()
{
    return this->index;
}

std::map<std::string, std::string> Scaler::getItems()
{
    return this->items;
}

std::string Scaler::getItemsStr()
{
    std::string itemStr = "";
    std::map<std::string, std::string>::iterator iter;
    for (iter = this->items.begin(); iter != this->items.end(); iter++)
    {
        itemStr = itemStr + iter->first + "=\"" + iter->second + "\",";
    }

    itemStr = itemStr.substr(0, itemStr.length() - 1);
    std::string result = "{" + itemStr + "}";

    return result;
}

std::string Scaler::toString()
{
    std::string itemStr = "";
    std::map<std::string, std::string>::iterator iter;
    for (iter = items.begin(); iter != items.end(); iter++)
    {
        itemStr = itemStr + iter->first + "=\"" + iter->second + "\",";
    }

    itemStr = itemStr.substr(0, itemStr.length() - 1);
    std::string result = index + "{" + itemStr + "}";

    return result;
}

void Scaler::clean()
{
    this->items.erase(this->items.begin(), this->items.end());
    this->items.clear();
    std::map<std::string, std::string>().swap(this->items);
    malloc_trim(0);
}